package com.lwx.sort;

public class BubbleSort {

  public static void main(String[] args) {
    int[] a = new int[] {5, 90, 3, 0, 1, 2, 7, 32, 22, 45};
    sort2(a);
    print(a);
  }

  private static void sort(int[] a) {
    for (int i = 0; i < a.length; i++) {
      for (int j = 0; j < a.length - i - 1; j++) {
        if (a[j + 1] < a[j]) {
          int tmp = a[j + 1];
          a[j + 1] = a[j];
          a[j] = tmp;
        }
      }
      print(a);
    }
  }

  private static void sort2(int[] a) {
    int temp = 0;
    for (int i = a.length - 1; i > 0; --i) {
      for (int j = 0; j < i; ++j) {
        if (a[j + 1] < a[j]) {
          temp = a[j];
          a[j] = a[j + 1];
          a[j + 1] = temp;
        }
      }
      print(a);
    }
  }

  static void print(int[] a) {
    for (int i = 0; i < 10; i++) {
      System.out.print(a[i] + ",");
    }
    System.out.println();
  }
}
